package com.yswl.yeeduo.ui.base;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.yswl.yeeduo.R;

public class BaseWebViewActivity2 extends BaseActivity implements OnTouchListener  {

	protected static final String TAG = BaseWebViewActivity2.class.getSimpleName();

	protected WebView webView;
	public RelativeLayout mLayout;// 为了使web Desroy后移除
	
	private FrameLayout videoview;// 全屏时视频加载view
	private View xCustomView;
	private String url = "http://mp.weixin.qq.com/s?__biz=MzIwNzA1NjM1NA==&mid=400115635&idx=1&sn=e3ad01595c73155ed2655c4cea14b738#rd";

	private WebChromeClient.CustomViewCallback 	xCustomViewCallback;
	private WebViewClient mWebViewClient = new WebViewClient() {

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			webView.getSettings().setBlockNetworkImage(false); //使用图片加载
		}

		@Override
		public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

		};

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		};
	};

	private WebChromeClient mWebChomeClient = new WebChromeClient() {
		private Bitmap xdefaltvideo;
		private View xprogressvideo;

		@Override
		// 播放网络视频时全屏会被调用的方法
		public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			webView.setVisibility(View.GONE);
			// 如果一个视图已经存在，那么立刻终止并新建一个
			if (xCustomView != null) {
				callback.onCustomViewHidden();
				return;
			}

			videoview.addView(view);
			xCustomView = view;
			xCustomViewCallback = callback;
			videoview.setVisibility(View.VISIBLE);
		}

		@Override
		// 视频播放退出全屏会被调用的
		public void onHideCustomView() {

			if (xCustomView == null)// 不是全屏播放状态
				return;

			// Hide the custom view.
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			xCustomView.setVisibility(View.GONE);

			// Remove the custom view from its container.
			videoview.removeView(xCustomView);
			xCustomView = null;
			videoview.setVisibility(View.GONE);
			xCustomViewCallback.onCustomViewHidden();

			webView.setVisibility(View.VISIBLE);

			// Log.i(LOGTAG, "set it to webVew");
		}

		// 视频加载添加默认图标
		@Override
		public Bitmap getDefaultVideoPoster() {
			// Log.i(LOGTAG, "here in on getDefaultVideoPoster");
			if (xdefaltvideo == null) {
				xdefaltvideo = BitmapFactory.decodeResource(getResources(), R.drawable.aio_image_default_round);
			}
			return xdefaltvideo;
		}

		// 视频加载时进程loading
		@Override
		public View getVideoLoadingProgressView() {
			// Log.i(LOGTAG, "here in on getVideoLoadingPregressView");

			if (xprogressvideo == null) {
				LayoutInflater inflater = LayoutInflater.from(BaseWebViewActivity2.this);
				xprogressvideo = inflater.inflate(R.layout.progressbar_horizontal, null);
			}
			return xprogressvideo;
		}

		// 网页标题
		@Override
		public void onReceivedTitle(WebView view, String title) {
			(BaseWebViewActivity2.this).setTitle(title);
		}


	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		setContentView(R.layout.activity_basewebview);

		makeData();
		initView();

		this.webView.loadUrl(url);
	}

	private void makeData() {
		getIntent();
	}

	private void initView() {
		mLayout = (RelativeLayout) findViewById(R.id.container);
		videoview = (FrameLayout) findViewById(R.id.video_view);
		webView = (WebView) findViewById(R.id.video_webview);
		videoview.setOnTouchListener(this);
		this.webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);// 指定滚动条样式
		this.webView.getSettings().setSupportZoom(true);// 设置允许缩放
		this.webView.getSettings().setJavaScriptEnabled(true);// 设置使用够执行JS脚本
		this.webView.getSettings().setBuiltInZoomControls(true);// 设置使支持缩放
		webView.getSettings().setBlockNetworkImage(true);// 图片下载阻塞
		this.webView.getSettings().setPluginState(PluginState.ON);// 有硬件插件 无否之
//		this.webView.getSettings().setPluginsEnabled(true);// 可以使用插件
		// this.webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		// //窗体自动使用屏幕
		this.webView.getSettings().setAllowFileAccess(true); // 设置是 否 不禁用
		
		this.webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);// 排版适应屏幕
		this.webView.getSettings().setUseWideViewPort(true);// 可任意比例缩放
		this.webView.getSettings().setLoadWithOverviewMode(true);// setUseWideViewPort方法设置webview推荐使用的窗口。setLoadWithOverviewMode方法是设置webview加载的页面的模式。
		this.webView.getSettings().setSavePassword(true);
		this.webView.getSettings().setSaveFormData(true);// 保存表单数据
		this.webView.getSettings().setGeolocationEnabled(true);// 启用地理定位
		this.webView.getSettings().setGeolocationDatabasePath("/data/data/org.itri.html5webview/databases/");// 设置定位的数据库路径
		this.webView.getSettings().setDomStorageEnabled(true);
		
		this.webView.setWebViewClient(this.mWebViewClient);
		webView.setWebChromeClient(this.mWebChomeClient);
	}

	protected void onPause() {
		super.onPause();
		this.webView.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.webView.onResume();
	}

	

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
			webView.goBack(); // goBack()表示返回WebView的上一页面
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_BACK) {
			this.finish();
			return true;
		}
		return false;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 以下代码防止 webView 报错
		this.mLayout.removeView(webView);
		webView.removeAllViews();
		webView.destroy();
	}

	/**
	 * 
	 * @param context
	 * @param __address
	 * @param navTitle
	 * @param isLanscape
	 *            是否横屏
	 * @param __hideNav隐藏标题bar
	 * @return
	 */
	public static Intent createIntent(Context context, String url, String navTitle, boolean isLanscape, boolean hideNav, Class<?> cls) {
		Intent returnIntent = BaseWebViewActivity2.createIntent(context, url, navTitle, isLanscape, cls);
		returnIntent.putExtra("hideNav", hideNav);
		return returnIntent;
	}

	public static Intent createIntent(Context context, String url, String navTitle, boolean isLanscape, Class<?> cls) {
		Intent returnIntent = new Intent();
		returnIntent.setClass(context, cls);
		returnIntent.putExtra("address", url);
		returnIntent.putExtra("navTitle", navTitle);
		returnIntent.putExtra("isLanscape", isLanscape);
		return returnIntent;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return false;
	}

}
